package aad.storyboard.controller;

import java.util.ArrayList;

import utils.ViewUtils;

import aad.storyboard.R;
import aad.storyboard.model.AnimationProvider;
import aad.storyboard.model.Chapter;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Story;
import aad.storyboard.model.StorySettings;
import aad.storyboard.model.menu.MenuItem;
import aad.storyboard.view.MenuViewCreator;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MenuTouchListener implements OnTouchListener, OnSeekBarChangeListener, OnCheckedChangeListener{

	private static MenuTouchListener instance = new MenuTouchListener();
	private Activity activity;
	private float startX = -1f;
	private float startY = -1f;
	private MenuViewProvider menuViewProvider;
	private StorySettings storySettings;
	
	private MenuTouchListener(){
		
	}
	
	public static MenuTouchListener getInstance(Activity act, MenuViewProvider viewProvider){
		instance.activity = act;
		instance.menuViewProvider = viewProvider;
		instance.storySettings = Story.getInstance().getSettings();
		return instance;
	}
	
	@Override
	
	/**
	 * Triggered when a button of the menu is touched, it considers the release gesture such that it can decide whether the touch was performed to scroll (x and y change) or to actually select an item of the menu. In the latter case, depending on the item, the respective action is performed).
	 */
	public boolean onTouch(View v, MotionEvent event) {
		int id = v.getId();
		int number = -1;
		Log.e("Tag",number+"");
		if(v.getTag() != null)
			number = ((Integer) v.getTag()).intValue();
		Log.e("Tag","touched"+number);
		System.out.print("id: "+id);
		View nextView = null;
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			this.startX = event.getX();
			this.startY = event.getY();
			return false;
		}
		else if (event.getAction() == MotionEvent.ACTION_UP && (Math.abs(event.getY() - this.startY)< ViewUtils.percentHeightToPixels(10))){
		switch(id){
		
			case MenuItem.Action.NONE: 
				break;
			
			case MenuItem.Action.EXIT:
				System.exit(0);
				break;
			
			case MenuItem.Action.CHAPTER:
				System.out.println("CHAPTERSELECTOION");
				nextView = menuViewProvider.getChapterMenuView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				
				activity.setContentView(nextView);
				break;
				
				
			case MenuItem.Action.CHAPTERSELECTION:
				System.out.println("CHAPTER");
				nextView = menuViewProvider.getSceneMenuView(number);
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				
				activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.SCENE:
				ArrayList<Chapter> chapters = Story.getInstance().getChapters();
				Chapter chapter = null;
				Scene scene = null;
				for(Chapter c : chapters){
					for(Scene s : c.getScenes()){
						if(s.getId() == number){
							chapter = c;
							scene =s;
						}
					}
				}
				if(scene != null){
					nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
					if(nextView.getParent() != null){
						((ViewGroup)nextView.getParent()).removeView(nextView);
					}
					
					activity.setContentView(nextView);
					ControllerManager.getInstance().startStory(scene);
				}
				break;
				
		
			case MenuItem.Action.MAIN_MENU:
				AudioPlayer.getInstance().stopAudio();
				if(Story.getInstance().getCurrentChapter() != null && Story.getInstance().getCurrentChapter().getCurrentScene() != null){
					AnimationProvider.getInstance().stopAllActiveAnimations();
					AnimationProvider.getInstance().undoAllAnimationsBackToTime(0);
				}
				View mainMenuView = menuViewProvider.getMainMenuView();
				activity.setContentView(mainMenuView);
				break;
			
			case MenuItem.Action.RESTART:
				System.out.println("Restarting");
				AudioPlayer.getInstance().stopAudio();
				AnimationProvider.getInstance().stopAllActiveAnimations();
				AnimationProvider.getInstance().undoAllAnimationsBackToTime(Story.getInstance().getCurrentChapter().getCurrentScene().getStartTime());
				Story.getInstance().setCurrentChapter(0);
				Story.getInstance().getCurrentChapter().setCurrentScene(0);
				AudioPlayer.getInstance().seekToPosition(0);
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(ControllerManager.getInstance().getGuiComposer().getMainView());
				ControllerManager.getInstance().startStory();
				break;
			
			case MenuItem.Action.START:
				//Log.e("Parent", " I already have a parent "+ ControllerManager.getInstance().getGuiComposer().getMainView().getParent().toString());
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				ControllerManager.getInstance().startStory();
				break;
				
			case MenuItem.Action.RESUME:
				System.out.println("Resuming");
				AudioPlayer.getInstance().playAudio();
				AnimationProvider.getInstance().unpauseAllAnimations();
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.SETTINGS:
				AudioPlayer.getInstance().stopAudio();
				AnimationProvider.getInstance().stopAllActiveAnimations();
				nextView = menuViewProvider.getSettingsMenuView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.BOOKMARKMENU:
						    nextView = menuViewProvider.getBookMarkMenuView();
							if(nextView.getParent() != null){
								((ViewGroup)nextView.getParent()).removeView(nextView);
							}
							activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.STORE_BOOKMARK:
				ControllerManager.getInstance().getAdapter().createBookmark(Story.getInstance().getCurrentChapter().getCurrentScene().getId(), null);
				break;
				
			case MenuItem.Action.BOOKMARKS:
				
				//record the starting coordinates
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					this.startX = event.getX();
					this.startY = event.getY();
				}
				
				//check whether it was a delete or a goto tab
				else if(event.getAction() == MotionEvent.ACTION_UP){
					if(this.startX == -1f || this.startY == -1f)
						break;
					
					float dx = Math.abs(this.startX - event.getX());
					if(dx > ViewUtils.getPixelsFromMillimetres(20)){
						//String sceneNo = Integer.toString(number);
						String text = ((TextView) v).getText().toString();
						text = text.substring(text.indexOf(",")+2);
						if(text.contains(",")){
							text = text.substring(0,text.indexOf(","));
						}
						System.out.println("Dele"+text);
						ControllerManager.getInstance().getAdapter().deleteBookmark(text);
						
					}
					else{
						ArrayList<Chapter> chapters1 = Story.getInstance().getChapters();
						Chapter chapter1 = null;
						Scene scene1 = null;
						for(Chapter c : chapters1){
							for(Scene s : c.getScenes()){
								if(s.getId() == number){
									chapter = c;
									scene1 =s;
								}
							}
						}
						if(scene1 != null){
							nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
							if(nextView.getParent() != null){
								((ViewGroup)nextView.getParent()).removeView(nextView);
							}
							
							activity.setContentView(nextView);
							ControllerManager.getInstance().startStory(scene1);
						}
					}
					this.startX = -1f;
					this.startY = -1f;
				}
			    nextView = menuViewProvider.getBookMarkMenuView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				break;
			}
		return true;
		}
		return false;
	}

	/**
	 * triggered when a settings button is hit
	 */
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		System.out.println("CHECKCHANGED: "+buttonView.getTag());
		int val = ((Integer) buttonView.getTag()).intValue();
		if(val == MenuItem.SCRIPT)
			this.storySettings.setScripts(isChecked);
		else if(val == MenuItem.WIKI)
			this.storySettings.setWikiMode(isChecked);
	}

	/**
	 * triggered if a bar is touched (e.g. volume bar)
	 */
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
System.out.println("SLIDER TOUCHED"+seekBar.getTag()+fromUser);

int val = ((Integer) seekBar.getTag()).intValue();
if(fromUser){
if(val == MenuItem.VOLUME)
	this.storySettings.setVolume(progress);
		
	}
	}
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {

		/*int val = ((Integer) seekBar.getTag()).intValue();
		if(val == MenuItem.VOLUME)
			this.storySettings.setVolume(seekBar.getProgress());*/
		
	}

}
